//  this library.
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:hktk_app/ienum/home/column_type_enum.dart';
import 'package:hktk_app/model/home/column_model.dart';

import 'package:hktk_app/pages/mine/more_paper_page.dart';
import 'package:hktk_app/pages/mine/more_video_page.dart';
import 'package:hktk_app/util/view_util.dart';
import 'package:hktk_app/widget/mine/bank_title_widget.dart';
import 'package:hktk_app/widget/mine/more_video_page.dart';
import 'package:hktk_app/widget/page_title_widget.dart';

/// 更多页面，首页更多，资源学习
class MorePage extends StatefulWidget {
  /// 标题名称
  final String titleName;

  /// 栏目相关数据
  final List<ColumnModel> list;

  const MorePage({
    super.key,
    required this.titleName,
    required this.list,
  });

  @override
  State<MorePage> createState() => _MorePageState();
}

class _MorePageState extends State<MorePage> with TickerProviderStateMixin {
  late BuildContext _context;

  /// tabBar 控制器
  late TabController _tabController;

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: widget.list.length, vsync: this);
  }

  @override
  void dispose() {
    super.dispose();

    _tabController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    _context = context;
    return Scaffold(
      backgroundColor: colorWhite1,
      // backgroundColor: colorBlack1,
      body: SafeArea(child: Column(
        children: [
          PageTitleWidget(
            name: widget.titleName,
            pageContext: context,
          ),
          hiSpace(height: 30),
          BankTitleWidget(
            tabController: _tabController,
            tabList: widget.list.map((v) => v.name!).toList(),
          ),
          hiSpace(height: 30),
          Expanded(child: _content()),
        ],
      )),
    );
  }

  /// 内容显示
  _content() {
    double bottom = ScreenUtil().bottomBarHeight;
    return Container(
      margin: EdgeInsets.only(bottom: bottom),
      child: TabBarView(
        controller: _tabController,
        children: _viewItems(),
      ),
    );
  }

  /// 内容页面显示
  _viewItems() {
    List<Widget> widgets = [];
    for (ColumnModel model in widget.list) {
      if (model.type == ColumnTypeEnum.paperLibrary ||
          model.type == ColumnTypeEnum.materialInfo) {
        // 试卷/资料
        widgets.add(MorePaperPage(
          columnId: model.id!,
          columnType: model.type!,
        ));
      } else if (model.type == ColumnTypeEnum.video) {
        // 视频，课程
        widgets.add(MoreVideoPage(columnId: model.id!));
      }
    }
    return widgets;
  }
}
